#include <iostream>
#include <string>
using std::string;
#include <map>
#include <algorithm>
#include "frameFactory.h"

FrameFactory::~FrameFactory() { 
  std::map<string, Frame*>::iterator pos = frames.begin(); 
  while ( pos != frames.end() ) {
	SDL_Surface* surface = pos->second->getSurface();
    SDL_FreeSurface(surface);
    delete pos->second;
    ++pos;
  }
  frames.clear();
  std::cout<<"delete framefactory"<<std::endl;
}

Frame* FrameFactory::getFrame(const string& name,const string& prefix) {
  std::map<string, Frame*>::iterator pos = frames.begin();
  Frame* frame; 
  while ( pos != frames.end() ) {
    if ( pos->first == name ) break;
    else ++pos;
  }
  if ( pos == frames.end() ) {
   frame = new Frame(gamedata->getXmlStr(name+"File"), 
      gamedata->getXmlInt(prefix+"Width"), gamedata->getXmlInt(prefix+"Height"),
      gamedata->getXmlInt(prefix+"SrcX"), gamedata->getXmlInt(prefix+"SrcY"),
      gamedata->getXmlBool(prefix+"Transparency")); 
    frames[name] = frame;
  }
  else {
    frame = pos->second;
  }
  return frame;
}

